package com.example.springboot_test.easyexcel.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.example.springboot_test.common.poi.constant.POIExcelConstant;
import com.example.springboot_test.common.poi.util.POIExcelUtil;
import com.example.springboot_test.easyexcel.model.SpinnerModel;
import com.example.springboot_test.easyexcel.model.common.SheetModel;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义下拉框处理器
 *
 * @author xudongmaster
 */
public class CustomSpinnerHandler implements SheetWriteHandler {
    /**
     * 下拉框信息列表
     */
    List<SpinnerModel> spinnerList;
    /**
     * sheet页名称列表
     */
    List<String> sheetNameList;

    /**
     * 构造方法
     *
     * @param spinnerList 下拉框信息列表
     */
    public CustomSpinnerHandler(List<SpinnerModel> spinnerList) {
        this.spinnerList = CollectionUtil.isNotEmpty(spinnerList) ? spinnerList.stream().filter(x ->
                StrUtil.isNotEmpty(x.getSheetName()) && x.getSpinnerData() != null
                        && x.getSpinnerData().length > 0
        ).collect(Collectors.toList()) : new ArrayList<>();
        this.sheetNameList = CollectionUtil.isNotEmpty(this.spinnerList) ? this.spinnerList.stream()
                .map(SheetModel::getSheetName).distinct().collect(Collectors.toList()) : new ArrayList<>();
    }

    /**
     * Sheet页创建之前
     *
     * @param writeWorkbookHolder 文档对象
     * @param writeSheetHolder    sheet页对象
     */
    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    /**
     * Sheet页创建之后
     *
     * @param writeWorkbookHolder 文档对象
     * @param writeSheetHolder    sheet页对象
     */
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        //不需要绑定下拉框，或者当前sheet页不需要绑定下拉框
        if (CollUtil.isEmpty(spinnerList) || !sheetNameList.contains(sheet.getSheetName())) {
            return;
        }
        //当前Sheet页的下拉框信息
        List<SpinnerModel> sheetSpinnerList = spinnerList.stream().filter(x ->
                StrUtil.equals(x.getSheetName(), sheet.getSheetName())).collect(Collectors.toList());
        //当前sheet页不需要绑定下拉框
        if (CollUtil.isEmpty(sheetSpinnerList)) {
            return;
        }
        //设置下拉框
        for (SpinnerModel spinnerModel : sheetSpinnerList) {
            //校验开始列索引大于结束列索引，或者开始行索引大于结束行索引
            if (spinnerModel.getStartColumnIndex() > spinnerModel.getEndColumnIndex() || spinnerModel.getStartRowIndex() > spinnerModel.getEndRowIndex()) {
                continue;
            }
            //校验开始列索引和结束列索引是否小于最小列索引
            if (spinnerModel.getStartColumnIndex() < POIExcelConstant.COL_INDEX_MIN || spinnerModel.getEndColumnIndex() < POIExcelConstant.COL_INDEX_MIN) {
                continue;
            }
            //校验开始行索引和结束行索引是否小于最小行索引，开始行索引和结束行索引是否大于最大行索引
            if (spinnerModel.getStartRowIndex() < POIExcelConstant.ROW_INDEX_MIN || spinnerModel.getEndRowIndex() < POIExcelConstant.ROW_INDEX_MIN
                    || spinnerModel.getStartRowIndex() > POIExcelConstant.ROW_INDEX_MAX || spinnerModel.getEndRowIndex() > POIExcelConstant.ROW_INDEX_MAX) {
                continue;
            }
            //绑定下拉框数据
            POIExcelUtil.setSpinnerData(writeWorkbookHolder.getWorkbook(), sheet, spinnerModel.getStartRowIndex(), spinnerModel.getEndRowIndex()
                    , spinnerModel.getStartRowIndex(), spinnerModel.getEndColumnIndex(), spinnerModel.getSpinnerData());
        }
    }
}